package com.csun.cmny.provider.device.relay;

import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.provider.device.watch.WatchNettyTcpServer;
import com.csun.cmny.provider.netty.NettyInitRunner;
import io.netty.bootstrap.ServerBootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-22 20:26
 * @Description: 网络继电器Netty Server配置
 */
@Slf4j
@Order(30)
@Configuration
@ConditionalOnProperty(prefix = "cmny.netty.relay", name = "switch", havingValue = "on")
public class RelayNettyConfig {

    @Resource
    private CmnyProperties cmnyProperties;
    @Resource
    private RelayNettyTcpChannelHandler relayNettyTcpChannelHandler;

    @Bean
    public RelayNettyTcpServer relayNettyTcpServer() {

        int port = cmnyProperties.getNetty().getRelay().getPort();

        log.info("### relay init runner starting...<<<<<<<<<<<<<");

        RelayNettyTcpServer relayNettyTcpServer =
                new RelayNettyTcpServer(port, relayNettyTcpChannelHandler, new ServerBootstrap());

        relayNettyTcpServer.init();

        NettyInitRunner.nettyServerMap.put(port, relayNettyTcpServer);

        log.info("### relay init runner finish...<<<<<<<<<<<<<");

        return relayNettyTcpServer;
    }

}
